//MOSTLY CONVERTED....UNTESTED

using System.Collections.Generic;
using System;
using UnityEngine;

public class Tower : MonoBehaviour{

public const int WINHEIGHT = 100;
public const int DECEPTION_DECAY_RATE = 2;
private List<Block> blocks;
 
	public void Awake()
	{
		blocks = new List<Block>();
	}
	
	public Tower(List<Block> blocks)
	{
		this.blocks = blocks;
	}
	
	
	public int getHeight()
	{
		int count = 0;
		
		foreach(Block b in blocks)
		{
			count += b.getHeight();
		}
		
		return count;
	}
	
	
	public void addBlock(Block b)
	{
		blocks.Add(b);
	}
	
	public void addBlockAt(int index, Block b){
		blocks.Insert(index,b);
	}
	
	
	public void removeBlock(Block b)
	{
		DestroyImmediate(b.gameObject,true);
		blocks.Remove(b);
	}
	
	public void removeBlockAt(int index){
		Block b= blocks[index];
		DestroyImmediate(b.gameObject,true);
		blocks.Remove(b);
	}
	
	public Block getTopBlock(){
		return blocks[blocks.Count-1];
	}
	
	public Block getBottomBlock(){
		return blocks[0];
	}
	
	public void turnTowerCheck(){
		//check for dead blocks
		for(int i=0; i<blocks.Count;i++){
			Block currentBlock= blocks[i];
			if (currentBlock.getHealth()<=0 || currentBlock.getHeight()<=0){
				removeBlockAt(i);
			}
			else if(currentBlock.getType()==3){
				int prevBlockType= 5;
				int nextBlockType= 6;
				try{
					Block prevBlock = blocks[i-1];
					prevBlockType=prevBlock.getType();
				}
				catch(IndexOutOfRangeException){
					
				}
				try{
					Block nextBlock = blocks[i+1];
					nextBlockType=nextBlock.getType();
				}
				catch(IndexOutOfRangeException){
					
				}
				if(prevBlockType==nextBlockType){
					Debug.Log("CONVERSION");
					int height= currentBlock.getHeight();
					int health=currentBlock.getHealth();
					removeBlockAt(i);
					this.gameObject.GetComponent<InstantiateBlocks>().MakeBlockAtIndex(prevBlockType,height,i);
					
				}
				else{
					currentBlock.takeDamage(DECEPTION_DECAY_RATE);
					if (currentBlock.getHealth()==0)
						removeBlockAt(i);
					
				}
				
			}
		}
		
		
	}
	
	
	public Block getBlock(int index)
	{
		if (index < 0)
		{
			index = 0;
		}
		if (index > blocks.Count)
		{
			index = blocks.Count;
		}
		return blocks[index];
	}
	
	
	public int getNumBlocks()
	{
		return blocks.Count;
	}
	
	public bool checkWin()
	{
		return (getHeight() > 99);
	}
	
	public bool checkLose()
	{
		return (blocks.Count == 0);
	}
	
	// public void drawTower(Graphics g, int x, int ybase)
	// {
		// Block b; int height = 0;
		// for(int i = 0; i < blocks.Count; i++)
		// {
			// b = blocks.get(i);
			// ybase -= b.getHeight()*4;
			// height += b.getHeight();
			// switch (b.getType())
			// {
				// default: case 0: 
				// g.setColor(new Color(0,0,Math.min(255,64+ 3*b.getHealth()))); break;
				
				// case 1:
				// g.setColor(new Color(Math.min(255,96+ 3*b.getHealth()),Math.min(255,96+ 3*b.getHealth()),0)); break;
				
				// case 2:
				// g.setColor(new Color(Math.min(128+4*b.getHealth(),255),50,50)); break;
				
				// case 3:
				// g.setColor(new Color(Math.min(255,4*b.getHealth()),Math.min(255,4*b.getHealth()),Math.min(255,4*b.getHealth()))); break;
			// }
			// g.fillRect(x, ybase, 100, b.getHeight()*4);
			// g.setColor(Color.white);
			// g.drawString("HP: "+ b.getHealth(), x, ybase+10);
			// g.drawString("Height: "+ b.getHeight(), x+40, ybase+10);
			// g.setColor(Color.black);
			// g.drawString(""+ height, x+100, ybase+6);
		// }
	// }
	
	public string toString()
	{
		return("Tower: height=" + getHeight() + " numBlocks=" + blocks.Count);
	}
	
	
	public void towerUpdate(){
		Transform towerTrans= this.gameObject.transform;
		float height=0;
		for(int i=0; i<blocks.Count;i++){
			Block b = blocks[i];
			Transform bTrans= b.gameObject.transform;
			float bWorldHeight= bTrans.localScale.y;
			bTrans.position= new Vector3(bTrans.position.x, towerTrans.position.y+height+(bWorldHeight/2),bTrans.position.z);
			height+=bWorldHeight;
		}
		
	}

}